﻿using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;

namespace CardPlay.DemoPack.Games.FreeCellVariants
{
    [Game("D388AC9A-AFBA-4954-BEED-DA5FA26CD30B", LevelOfDifficulty.Medium, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class EightOff : FreeCellBase
    {
        public EightOff()
        {
            TotalTableaus = 8;
            TotalReserves = 8;
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var metrics = presentation.Metrics;

            var root = new GridLayout
                       {
                           Columns = 3,
                           Rows = 1,
                           HorizontalContentAlignment = HorizontalAlignment.Center,
                           VerticalContentAlignment = VerticalAlignment.Stretch
                       };

            {
                var foundations = presentation.CreateColumnLayout(); // CreateSideBySideLayout(metrics);
                //foundations.Direction = SpreadDirection.Vertical;

                foreach (var name in PilesNamed(AnyFoundation).Select(pile => pile.Name))
                    foundations.Add(presentation.CreatePileVisual(name, StandardStyles.Foundation));

                root.Add(foundations);
            }

            root.Add(presentation.CreateMimimalSpacer());

            {
                var leftArea = new SideBySideLayout
                               {
                                   HorizontalContentAlignment = HorizontalAlignment.Stretch,
                                   Direction = SpreadDirection.Vertical,
                                   MinimumSpacing = metrics.MinSpacingDown,
                                   MaximumSpacing = metrics.MinSpacingDown
                               };

                {
                    var reserve = presentation.CreateRowLayout(); // CreateSideBySideLayout(metrics);
                    var style = new VisualTraits
                                {
                                    EmptyCue = () => EmptyPileCue.CardSite,
                                    Arrangement = CardArrangement.StackSquare,
                                    ContentFit = ContentFit.FitHeight
                                };

                    foreach (var pile in PilesNamed(AnyReserve))
                        reserve.Add(presentation.CreatePileVisual(pile.Name, style));

                    leftArea.Add(reserve);
                }

                {
                    var tableau = presentation.CreateRowLayout(); // CreateSideBySideLayout(metrics);
                    //tableau.VerticalContentAlignment = VerticalAlignment.Top;

                    foreach (var pile in PilesNamed(AnyTableau))
                        tableau.Add(presentation.CreatePileVisual(pile.Name, StandardStyles.Tableau));

                    leftArea.Add(tableau);
                }

                root.Add(leftArea);
            }

            return root;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            foreach (var pile in PilesNamed("reserve.[1-4]").Select(pile => pile.Name))
                script.Deal("1 up").To(pile);

            foreach (var pile in PilesNamed(AnyTableau).Select(pile => pile.Name))
                script.Deal("6 up").To(pile);
        }
    }
}